﻿//模块
var MEUN_LIST;
var MENU_PAIRS;

$(function () {
    try {
        setMenuTree(true);
    }
    catch (e) { }
});

//初始化菜单缓存
var setMenuTree = function (init) {

    try {
        if (init) {

            //获取模块列表
            MENU_LIST = getMenuList();

            //保存缓存
            localStorage.setItem("menulist", JSON.stringify(MENU_LIST));
        }
        else {

            if (localStorage.getItem("menulist") != undefined && localStorage.getItem("menulist") != null && localStorage.getItem("menulist") != "undefined") {
                //从缓存读取数据
                MENU_LIST = JSON.parse(localStorage.getItem("menulist"));
            }
            else {
                //获取模块列表
                MENU_LIST = getMenuList();

                //保存缓存
                localStorage.setItem("menulist", JSON.stringify(MENU_LIST));
            }
        }

        if (MENU_PAIRS == null || MENU_PAIRS == undefined || MENU_PAIRS.length == 0) {
            //获取编码表的键值对
            setMenuPairs();
        }

        var vMenuHtml = getMenu(MENU_LIST);

        document.getElementById("left-menu").innerHTML = vMenuHtml;
    }
    catch (e) {

    }
};


//获取模块列表
var getMenuList = function () {
    try {
        let xheList = new XheList();

        xheList.model.Table = "t_code_model";
        xheList.model.SortName = "sortid";
        xheList.model.Columns = [
            ["modelcode", "ModelCode", "", "", "C", "", true],
            ["parentcode", "ParentCode", "", "", "C", "", true],
            ["title", "Title", "", "", "C", "", true],
            ["url", "Url", "", "", "C", "", true],
            ["icon", "Icon", "", "", "C", "", true],
            ["params", "Params", "", "", "C", "", true],
            ["event", "Event", "", "", "C", "", true],
            ["target", "Target", "", "", "C", "", true],
            ["appid", "appid", "", "", "C", "", true]
        ];
        xheList.model.Filter = [
            ["Status", "A", "EQ", "I", 1]
        ];

        let vResult = xheList.query();
        
        return vResult.rows;
    }
    catch (e) {
        return null;
    }
};

//获取菜单键值对
var getMenuPairs = function () {
    var vLocalPath = window.location.pathname;


    var vKey = "";

    for (var i = 0; i < MENU_PAIRS.length; i++) {

        if (MENU_PAIRS[i].url.indexOf(vLocalPath) > -1) {
            vKey = MENU_PAIRS[i].key;
            break;
        }
    }

    return vKey;
};

//设置菜单键值对
var setMenuPairs = function () {

    MENU_PAIRS = new Array();

    for (var i = 0; i < MENU_LIST.length; i++) {

        var vMenu = {
            url: MENU_LIST[i].url,
            key: MENU_LIST[i].modelcode,
            appid: MENU_LIST[i].appid
        };

        MENU_PAIRS.push(vMenu)

    }
};

var jumpUrl = function (code, params1, params2, params3, params4, params5) {

    if (params1 == undefined) {
        params1 = "";
    }
    if (params2 == undefined) {
        params2 = "";
    }
    if (params3 == undefined) {
        params3 = "";
    }
    if (params4 == undefined) {
        params4 = "";
    }
    if (params5 == undefined) {
        params5 = "";
    }

    var vUrl = "";
    var vParams = {
        params1: params1,
        params2: params2,
        params3: params3,
        params4: params4,
        params5: params5
    };

    for (var i = 0; i < MENU_PAIRS.length; i++) {
        if (MENU_PAIRS[i]["key"] == code) {
            vUrl = MENU_PAIRS[i]["url"];
            break;
        }
    }

    if (vUrl != "") {
        HttpUtil.setUrlParams(vUrl, vParams);
    }
    else {
        alert("该功能未实现!");
    }
};


var getMenu = function (menuList) {

    var vMenuList = setMenuList(menuList);

    var vHtml = "";
    vHtml += "<ul id='main-nav' class='nav nav-tabs nav-stacked'>";


    for (var i = 0; i < vMenuList.length; i++) {

        vHtml += getChildMenu(vMenuList[i], 0);
    }
    vHtml += "<li>";
    vHtml += "<a href='#'  onclick='closeMenu()'>";
    vHtml += "<i class='glyphicon glyphicon-remove'></i>&nbsp;&nbsp;关闭";
    vHtml += "</a>";
    vHtml += "</li>";
    vHtml += "</ul>";

    return vHtml;
};



var setMenuList = function (menuList) {

    var _MenuList = new Array();

    for (var i = 0; i < menuList.length; i++) {
        if (menuList[i].parentcode == "0") {

            var vMenu = {
                title: menuList[i].title,
                url: menuList[i].url,
                key: menuList[i].modelcode,
                icon: menuList[i].icon,
                params: menuList[i].params,
                event: menuList[i].event,
                child: new Array()
            };

            _MenuList.push(vMenu)
        }
    }

    for (var i = 0; i < _MenuList.length; i++) {
        for (var j = 0; j < menuList.length; j++) {
            if (_MenuList[i].key == menuList[j].parentcode) {
                var vMenu = {
                    title: menuList[j].title,
                    url: menuList[j].url,
                    key: menuList[j].modelcode,
                    icon: menuList[j].icon,
                    params: menuList[j].params,
                    event: menuList[j].event,
                    child: new Array()
                };


                for (var n = 0; n < menuList.length; n++) {
                    if (vMenu.key == menuList[n].parentcode) {
                        var vMenuChild = {
                            title: menuList[n].title,
                            url: menuList[n].url,
                            key: menuList[n].modelcode,
                            icon: menuList[n].icon,
                            params: menuList[n].params,
                            event: menuList[n].event,
                            child: new Array()
                        };

                        vMenu.child.push(vMenuChild);
                    }
                }

                _MenuList[i].child.push(vMenu)
            }
        }
    }

    return _MenuList;
};

var getChildMenu = function (childMenu, childIndex) {

    var vHtml = "";

    if (childMenu.title != "") {
        if (childMenu.child.length > 0) {
            vHtml += "<li>";
            vHtml += "<a href='#" + childMenu.event + "' class='nav-header collapsed' data-toggle='collapse'>";

            for (var i = 0; i < childIndex; i++) {
                vHtml += "&nbsp;&nbsp;&nbsp;&nbsp;";
            }

            vHtml += "<i class='glyphicon " + childMenu.icon + "'></i>";
            if (childMenu.url == "#") {
                vHtml += "&nbsp;&nbsp;" + childMenu.title + "";
            }
            else {
                vHtml += "<span onclick='jumpUrl(" + childMenu.key + "," + childMenu.params + ")'>&nbsp;&nbsp;" + childMenu.title + "</span>";
            }
            vHtml += "<span class='pull-right glyphicon glyphicon-triangle-bottom'></span>";
            vHtml += "</a>";
            vHtml += "<ul id='" + childMenu.event + "' class='nav nav-list collapse secondmenu' style='height:0px;'>";



            if (childMenu.child.length > 0) {
                for (var j = 0; j < childMenu.child.length; j++) {
                    vHtml += getChildMenu(childMenu.child[j], (childIndex + 1));
                }
            }
            vHtml += "</ul>";
            vHtml += "</li>";
        }
        else {
            vHtml += "<li>";
            vHtml += "<a href='" + childMenu.url + "'>";
            for (var i = 0; i < childIndex; i++) {
                vHtml += "&nbsp;&nbsp;&nbsp;&nbsp;";
            }
            vHtml += "<i class='glyphicon " + childMenu.icon + "'></i>&nbsp;&nbsp;" + childMenu.title + "</a></li>";
        }
    }

    return vHtml;
};

var closeMenu = function () {
    $("#left-menu").fadeOut(500);
};

var showMenu = function () {
    $("#left-menu").fadeIn(1000);
};